package org.elanfox.intellicommunity.intellicommunityadmin.controller.equipment;

import org.elanfox.intellicommunity.biz.dto.equipment.LocatorAreaSaveDTO;
import org.elanfox.intellicommunity.biz.entity.equipment.LocatorAreaPO;
import org.elanfox.intellicommunity.biz.service.core.equipment.LocatorAreaService;
import org.elanfox.intellicommunity.biz.vo.LocatorAreaVO;
import org.elanfox.intellicommunity.common.base.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author allan
 * @version 1.0.0
 * @date 2/19/21
 */
@Controller
@RequestMapping("/locator/area")
public class LocatorAreaController {
    @Autowired
    private LocatorAreaService locatorAreaService;

    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("list", locatorAreaService.findAllArea());

        return "equipment/locator_area_list";
    }

    @GetMapping("/api/list")
    @ResponseBody
    public ApiResult<List<LocatorAreaVO>> list() {
        List<LocatorAreaPO> pos = locatorAreaService.findAll();

        List<LocatorAreaVO> vos = pos.stream().map(LocatorAreaVO::new).collect(Collectors.toList());

        return ApiResult.success(vos);
    }

    @PostMapping("/save")
    @ResponseBody
    public ApiResult<Void> save(LocatorAreaSaveDTO dto) {
        locatorAreaService.save(dto);

        return ApiResult.success();
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public ApiResult<Void> delete(@PathVariable("id") Integer id) {

        return ApiResult.success();
    }
}
